package com.eascs.admin.o2o.recommend.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.eascs.admin.o2o.internal.controller.PayMentController;
import com.eascs.admin.o2o.internal.util.BaseUserUtil;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.recommend.entity.O2oRecommendEntity;
import com.eascs.admin.o2o.recommend.service.O2oRecommendService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.web.url.UrlHelper;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.service.sys.SysOrgService;

import net.sf.json.JSONObject;

@RestController
@RequestMapping("/rrecommend")
public class O2oRecommendRestController {
	private static Logger log = LoggerFactory.getLogger(O2oRecommendRestController.class);

	@Autowired
	O2oRecommendService o2oRecommendService;
	
	@Autowired
	PayMentController payMentController;
	
	@Reference
	SysOrgService service;
	
	@Autowired
	BaseUserUtil userUtils;
	/**
	 * @Description: 查询登录人信息
	 * @return json string
	 * @throws @date
	 * 2018年2月28日 上午11:07
	 */
/*	@RequestMapping(value = "/userdate")
	public String findProvince() {
		User user = UserHolder.getUser();
		return JSON.toJSONString(user);

	}*/
	
	@RequestMapping(value = "/userdate")
    public void hello(HttpServletResponse response) {  
        User user = UserHolder.getUser();
        //do something    
        //使用Json返回json格式数据  
        JSONObject josn = new JSONObject();  
        response.setCharacterEncoding("UTF-8");  
        response.setContentType("text/html;charset=UTF-8");  
        try {
			response.getWriter().print(JSON.toJSONString(user));
		} catch (IOException e) {
			e.printStackTrace();
		}  
    }  
	
	/**
	 * 保存申请征信信息
	 * type 1或是空。保存，
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/saveO2oRecommend", method = RequestMethod.POST)
	public BaseRestResult<String> addO2oZx(O2oRecommendEntity o2oRecommendEntity,String type) {
		
		if(o2oRecommendEntity.getSqType()==null){
			return BaseRestResult.warn("请选择当前申请人类型");
		}
		
		if(o2oRecommendEntity.getUserName()==null){
			return BaseRestResult.warn("客户姓名不能为空");
		}
		
		if(o2oRecommendEntity.getUserPhone()==null){
			return BaseRestResult.warn("客户联系方式不能为空");
		}
		
		if(o2oRecommendEntity.getSqType().equals(StatusContexts.RECOMMENDSQ_TYPE_O)){
			if(o2oRecommendEntity.getReferrerNo()==null){
				return BaseRestResult.warn("请联想选择推荐人");
			}
			if(o2oRecommendEntity.getReferrerProvinceId()==null){
				return BaseRestResult.warn("请选择推荐人省区");
			}
		}else{
			if(o2oRecommendEntity.getOperatorNo()==null){
				return BaseRestResult.warn("请联想选择经办人");
			}
			if(o2oRecommendEntity.getOperatorProvinceId()==null){
				return BaseRestResult.warn("请选择经办人省区");
			}
		}
		
		if(o2oRecommendEntity.getRecommendType()==null){
			return BaseRestResult.warn("请选择推荐类型");
		}
		
		log.info("保存o2oRecommendEntity=={}", JSON.toJSONString(o2oRecommendEntity));
		if(!"2".equals(type)){
			o2oRecommendService.saveorupdateEntity(o2oRecommendEntity);
			return BaseRestResult.info("保存成功").andRedirect(UrlHelper.build("/recommend/detail?id=" + o2oRecommendEntity.getId()));
		}else{
			return payMentController.saveRecommendStart(o2oRecommendEntity);
		}
		
	}
	
	
	/**
	 * @Description:
	 * 				<p>
	 *               客户推荐详情
	 *               </p>
	 * @author yy
	 * @date 2017年11月30日 
	 * @version V1.0
	 */
	@RequestMapping("/detail")
	public BaseRestResult<String> detailInfo(String id, String type,String idCard) {

		O2oRecommendEntity o2oRecommendEntity  = o2oRecommendService.getById(id);
		List<ActOrgEntity> orgProvince = service.getOrgProvinceAndHQ();
		ViewContext.put("ActOrgEntitys", orgProvince);
		ViewContext.put("o2oRecommendEntity", o2oRecommendEntity);
		
		return new HtmlRestResult("recommend/rrecommendDetail_nq");
	}
	
	/**
	 * @Description:
	 * 				<p>
	 *               删除客户推荐信息
	 *               </p>
	 * @author yy
	 * @date 2018年3月1日 下午2:39:19
	 * @version V1.0
	 */
	@RequestMapping("/delete")
	public BaseRestResult<String> deleteO2oZx(String id,String type) {
		O2oRecommendEntity o2oRecommendEntity = o2oRecommendService.getById(id);
		String message = "";
		if(StatusContexts.RECOMMENDSQ_ISTYPE_D.equals(type)){
			message="删除";
		}else if(StatusContexts.RECOMMENDSQ_ISTYPE_T.equals(type)){
			message="作废";
		}
		o2oRecommendEntity.setIsType(type);
		int i = o2oRecommendService.updateById(o2oRecommendEntity);
		if (i > 0) {
			return BaseRestResult.info(message+"成功").andRedirect("/recommend/list");
		}
		return BaseRestResult.warn(message+"失败！");
	}
	
	/**
	 *  显示客户推荐管理查询详情客户联系电话和证件号码
	 * @param id
	 * @return
	 */
	@RequestMapping("/show_window")
	public BaseRestResult<String> show_window(String id) {
		O2oRecommendEntity o2oRecommendEntity = o2oRecommendService.getById(id);
		ViewContext.put("o2oRecommendEntity", o2oRecommendEntity);
		return HtmlRestResult.popHtmlFragment("/recommend/rec_show_info");
	}
	

}
